package ai.people.netmon.framework.domain.searchmsg.vo;

import ai.people.netmon.utils.StringPool;
import ai.people.netmon.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yuanqinglong
 * @date 2022/8/4 18:38
 */
@Data
public class EsWarningMessageVO {


    /**
     * id
     */
    private String id;

    /**
     * 信息链接
     */
    private String messageLink;

    /**
     * 情感标签
     *
     * @mock 中性
     */
    private String emotion;

    /**
     * 业务标签
     *
     * @mock 反政府
     */
    private String label;

    /**
     * 账户
     */
    private String account;

    /**
     * 账户昵称
     */
    private String accountNickName;

    /**
     * 帐户id
     */
    private String accountId;

    /**
     * 账户简介
     */
    private String accountSummary;

    /**
     * 群昵称
     */
    private String groupNickName;

    /**
     * 群成员
     */
    private String groupMember;

    /**
     * 群账户
     */
    private String groupAccount;

    /**
     * 群组id
     */
    private String groupId;

    /**
     * 群简介
     */
    private String groupSummary;

    /**
     * 发布时间
     *
     * @mock 2022-02-02 10:01:01
     */
    private String releaseTime;

    /**
     * 收集时间
     *
     * @mock 2022-02-02 10:01:01
     */
    private String gatherTime;

    /**
     * 危险程度
     *
     * @mock 高危
     */
    private String dangerous;

    /**
     * 平台来源
     */
    private String platform;

    /**
     * 发布人
     */
    private String publisher;

    /**
     * 转载于
     */
    private String reproducedIn;

    /**
     * 内容
     */
    private String content;

    /**
     * 标题
     */
    private String title;


    /**
     * 构建
     *
     * @param content 内容
     * @return {@link EsWarningMessageVO}
     */
    public static EsWarningMessageVO build(Map content) {
        EsWarningMessageVO esWarningMessageVO = new EsWarningMessageVO();
        esWarningMessageVO.setId((String) content.get("id"));
        esWarningMessageVO.setContent((String) content.get("content"));
        esWarningMessageVO.setAccount((String) content.get("user_name"));
        esWarningMessageVO.setAccountId((String) content.get("user_id"));
        esWarningMessageVO.setAccountNickName((String) content.get("nick_name"));
        esWarningMessageVO.setGroupId((String) content.get("group_id"));
        esWarningMessageVO.setPlatform((String) content.get("info_type"));
        String releaseTime = StringUtils.formatDate("yyyy-MM-dd HH:mm:ss", (String) content.get("date"));
        String gatherTime = StringUtils.formatDate("yyyy-MM-dd HH:mm:ss", (String) content.get("ts"));
        esWarningMessageVO.setReleaseTime(releaseTime);
        esWarningMessageVO.setGatherTime(gatherTime);
        esWarningMessageVO.setMessageLink((String) content.get("url"));
        Object tags = content.get("tag");
        if (Objects.nonNull(tags)) {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(tags));
            String tagsNames = jsonObject.getJSONArray("tag").stream().map(String::valueOf).collect(Collectors.joining(StringPool.COMMA));
            esWarningMessageVO.setLabel(tagsNames);
        }
        esWarningMessageVO.setTitle((String) content.get("title"));
        esWarningMessageVO.setEmotion((String) content.get("sentiment"));
        return esWarningMessageVO;
    }
}
